<template>
  <div>
    <form-com :form="searchform" :provinceList="provinceList" @onSubmit="onSubmit" />
    <el-table
      v-loading="listLoading"
      :data="dataList"
      stripe
      border
      row-key="id"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="序号" align="center" type="selection" width="50" />
      <el-table-column label="序号" align="center" type="index" width="100" />
      <el-table-column label="日期" align="center" width="100" />
      <el-table-column label="昵称" align="center" prop="nickName" width="140">
        <!-- <template slot-scope="{ row }">
          <span>{{ examType[row.examType] }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="省份" align="center" prop="provinceName" width="100" />
      <el-table-column label="市" align="center" prop="cityName" width="100" />
      <el-table-column label="医院名称" align="center" prop="hospitalName" width="180" />
      <el-table-column label="第一轮评审" align="center" prop="review" width="140">
        <template slot-scope="{ row }">
          <span v-if="row.examineStatus === 1">已评审</span>
          <span v-if="row.examineStatus === 2">未评审</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="考核时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.beginTime | parseTime('{y}-{m}-{d}') }} - {{ row.endTime | parseTime('{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="发布状态" align="center">
        <template slot-scope="{row}">
          <span v-if="row.publishState===1">已发布</span>
          <span v-else>未发布</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button type="text">
            <span @click="lookDetail(row)">查看详情</span>
          </el-button>
          <el-button v-if="row.status === 1" type="text" @click="submitJudges(row)"
            >提交评委</el-button
          >

          <el-button type="text" @click="handleUpdate(row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-button type="primary" class="botttom" @click="judgesVisible = true">分配评委</el-button>

    <!-- 分配评委 -->
    <el-dialog title="分配评委" :visible.sync="judgesVisible" width="600px" append-to-body>
      <el-form>
        <el-form-item label="选择评委">
          <!-- <el-input width="100" placeholder="请输入评委名称"></el-input> -->
          <el-select v-model="examineUser">
            <el-option :value="item.id" :label="item.name" v-for="item of users" :key="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="onModalFormCancel">取 消</el-button>
        <el-button v-if="!editShow" :loading="submitLoading" type="primary" @click="onAssign"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { assessmentSettingsUpdate } from '@/api/skinDisease/collocate/assess'
import formCom from './../form/form.vue'
import { csExaList, csaddExamineUser, getUsers } from '@/api/skinDisease/case/caseApis.js'

export default {
  name: 'index',
  components: {
    formCom
  },
  props: ['provinceList'],
  data() {
    return {
      searchform: {
        starTime: '',
        endTime: '',
        provinceId: '',
        cityId: '',
        hosphital: '',
        // 审核状态
        examineStatus: '',
        // 查询类型
        searchType: 2,
        selectRows: []
      },
      pageNum: 1,
      pageSize: 10,
      total: 0,
      dataList: [],
      listLoading: false,
      // 新增/编辑用户
      editShow: false,
      formVisible: false,
      submitLoading: false,
      examAddress: [{}],
      formModel: {},
      judgesVisible: false,
      users: [],
      examineUser: '',
      type: ''
    }
  },
  methods: {
    distribution() {
      this.$emit('distribution')
      console.log('distribution')
    },
    Judges() {
      this.judgesVisible = true
    },
    submitJudges({ comments, examineStatus, examineUser, id }) {
      csaddExamineUser({ comments, examineStatus, examineUser, id }).then(() => {
        this.$message.success('操作成功')
      })
    },
    lookDetail(row) {
      console.log('rows', row)
      this.$router.push({
        path: '/skinDisease/caseDetail',
        query: {
          type: 'one',
          id: row.id,
          name: row.nickName,
          examineUser: row.examineUser,
          examineStatus: row.examineStatus,
          comments: row.comments
        }
      })
    },
    // 搜索列表
    handleQuery() {
      this.listLoading = true

      const { starTime, endTime, examineStatus, provinceId, searchType } = this.searchform

      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        starTime,
        endTime,
        searchType
      }
      if (examineStatus) params.examineStatus = examineStatus
      if (provinceId) params.provinceId = provinceId

      csExaList(params)
        .then(res => {
          this.dataList = res.data.records
          this.listLoading = false
          this.total = res.data.total
          console.log('res', res)
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    // 发布/取消发布
    handleState({ id, publishState }) {
      this.$confirm(`请确认是否要${publishState === 1 ? '发布' : '取消发布'}该规则?`, {
        type: 'warning'
      }).then(async () => {
        try {
          await assessmentSettingsUpdate({ id, publishState: publishState === 1 ? 0 : 1 })
          this.$message.success(`${publishState === 1 ? '取消' : '发布'}成功`)
          this.handleQuery()
        } catch (error) {
          console.log(error)
        }
      })
    },
    handleUpdate(row) {
      this.type = 'edit'
      this.lookDetail(row)
    },
    // 新增/编辑提交
    submitForm() {},

    reset() {
      this.formModel = {
        id: '',
        name: ''
      }
      this.resetForm('formModel')
    },
    onSubmit() {
      this.handleQuery()
    },
    queryUsers() {
      getUsers(23).then(res => {
        this.users = res.data
      })
    },
    handleSelectionChange(v) {
      this.selectRows = v
    },
    onAssign() {
      for (const item of this.selectRows) {
        csaddExamineUser({ examineUser: this.examineUser, id: item.id }).then(() => {
          this.$message.success('分配成功')
          this.judgesVisible = false
          this.examineUser = ''
        })
      }
    },
    onModalFormCancel() {
      this.judgesVisible = false
    }
  },
  mounted() {
    this.handleQuery()
    this.queryUsers()
  }
}
</script>

<style lang="scss" scoped>
.address {
  .address-item {
    display: flex;
    &:not(:last-child) {
      margin-bottom: 10px;
    }
    .btn {
      flex-shrink: 0;
      width: 44px;
      height: 32px;
    }
  }
}

.botttom {
  margin-top: 20px;
}
</style>
